//
//  PostTopicApi.m
//  CoollangTennisBall
//
//  Created by Coollang on 2017/9/12.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFPostTopicApi.h"
#import "NSString+Extension.h"

// 帖子图片上传获取七牛的token
static NSString * kGetQiNiuTokenUrl = @"Tsns/getUpToken";
// 发帖
static NSString * kPostTopicUrl = @"Tsns/post";

@interface YFPostTopicApi ()

/** 经纬度坐标:先经度后纬度用,隔开 */
@property (nonatomic, copy)NSString *position;
/** 主题：先留空,万一以后有发表主题的功能 */
@property (nonatomic, copy)NSString *subject;
/** 内容 */
@property (nonatomic, copy)NSString *content;
/** 上传到七牛后的图片链接 */
@property (nonatomic, strong) NSArray *imageUrlList;
/** 是发帖还是获取Token */
@property (nonatomic, assign) BOOL isPostTopic;

@end

@implementation YFPostTopicApi

- (instancetype)initWithPosition:(NSString *)position imgList:(NSArray *)imageUrlList subject:(NSString *)subject textContent:(NSString *)content {
    if (self = [super init]) {
        self.position = position;
        self.imageUrlList = imageUrlList;
        self.subject = subject;
        self.content = content;
        _isPostTopic = YES;
    }
    return self;
}

- (NSString *)requestUrl {
    if (_isPostTopic) {
        return kPostTopicUrl;
    }else {
          return kGetQiNiuTokenUrl;
    }
}

- (id)requestArgument {
    if (_isPostTopic) {
        return @{
                 @"position":[NSString checkIfNullWithString:_position],
                 @"imgList":(self.imageUrlList == nil ? @[]:self.imageUrlList),
                 @"subject":_subject == nil ? @"主题-网球iOS":_subject,
                 @"text":[NSString checkIfNullWithString:_content],
                 };
    }else {
        return @{};
    }
}

+ (void)getQiNiuTokenCompleted:(void(^)(NSString *token))block {
    YFPostTopicApi *api = [[YFPostTopicApi alloc] init];
    api.isPostTopic = NO;
    [[YFNetworkingService sharedInstance] sendRequest:api callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            block(api.responseInfo[@"token"]);
        }else {
            block(nil);
        }
    }];
}


@end
